<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>动画</title>
    <style>
      #d1 {
        width: 50px;
        height: 50px;
        background-color: red;
        position: absolute;
        /* transition: all 5s; */
        left: 0;
        top: 0;
      }
    </style>
  </head>
  <body>
    <div id="d1"></div>
    <button style="margin-left: 100px">按钮</button>
    <script>
      //js实现
      let dv = document.getElementById("d1");
      let pos = 0;
      document.getElementsByTagName("button")[0].onclick = function () {
        let num = setInterval(() => {
          pos++;
          dv.style.left = pos + "px";
          dv.style.top = pos + "px";
          if (pos > 500) {
            console.log(num);
            clearInterval(num);
          }
        }, 10);
      };
    </script>
  </body>
</html>
